package top.scsoul.pas.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.scsoul.pas.bean.XhrResponse;
import top.scsoul.pas.entity.Clazz;
import top.scsoul.pas.service.ClazzService;

import java.util.List;

@Slf4j
@Controller
public class ClazzController {
    @Autowired
    ClazzService clazzService;

    /**
     *
     * @param currentPage
     * @param pageSize
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN')")
    @RequestMapping("clazz/list")
    public String clazzList(
            @RequestParam(required = false) int currentPage,
            @RequestParam(required = false) int pageSize,
            Model model) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        Page<Clazz> clazzPage = clazzService.listClazz(currentPage, pageSize);
        model.addAttribute("clazzPage", clazzPage);
        return "class_list";
    }
    @PreAuthorize("hasAnyRole('ADMIN')")
    @RequestMapping("clazz/add")
    public String addClazzPage() {
        return "class_add";
    }

    /**
     *
     * @param id
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN')")
    @RequestMapping("clazz/edit/{id}")
    public String editClazzPage(@PathVariable Long id, Model model) {
        Clazz clazz = clazzService.getById(id);
        model.addAttribute("clazz", clazz);
        return "class_edit";
    }

    /**
     *
     * @param name
     * @param created
     * @param leaderId
     * @param type
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN')")
    @ResponseBody
    @RequestMapping("api/addClazz")
    public XhrResponse addClazz(String name, String created, long leaderId,Integer type) {
        try {
            return XhrResponse.success(clazzService.save(name, created, leaderId,type));
        } catch (Exception e) {
            return XhrResponse.error("");
        }
    }

    /**
     *
     * @param id
     * @param name
     * @param created
     * @param leaderId
     * @param type
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN')")
    @ResponseBody
    @RequestMapping("api/updateClazz")
    public XhrResponse updateClazz(Long id, String name, String created, long leaderId,Integer type) {
        try {
            clazzService.update(id, name, created, leaderId, type);
        } catch (Exception e) {
            return XhrResponse.error("");
        }
        return XhrResponse.success();
    }
    @PreAuthorize("hasAnyRole('ADMIN')")
    @ResponseBody
    @RequestMapping("api/deleteClazz")
    public XhrResponse deleteClazz(Long id) {
        clazzService.delete(id);
        return XhrResponse.success();
    }

    /**
     *
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN','TEACHER')")
    @ResponseBody
    @RequestMapping("api/clazzList")
    public XhrResponse clazzListApi() {
        List<Clazz> clazzList = clazzService.listClazz();
        return XhrResponse.success(clazzList);
    }
}
